package io.ticktick.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.ticktick.common.utils.R;
import io.ticktick.modules.app.annotation.Login;
import io.ticktick.modules.sys.entity.IpInfoEntity;
import io.ticktick.modules.sys.service.IpInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("sys/userip")
public class IpInfoController {

    @Autowired
    private IpInfoService ipInfoService;

    @Login
    @GetMapping("/list/{id}")
    public R list(@PathVariable("id") Long id){

        List<IpInfoEntity> list = ipInfoService.list(new QueryWrapper<IpInfoEntity>().eq("user_id", id).orderByAsc("create_time"));

        return R.ok().put("list", list);
    }


    /**
     * 信息
     */
    @Login
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        IpInfoEntity ipInfoEntity = ipInfoService.getById(id);

        return R.ok().put("ipInfoEntity", ipInfoEntity);
    }

    /**
     * 保存
     */
    @Login
    @RequestMapping("/save")
    public R save(@RequestBody IpInfoEntity ipInfo){
        ipInfoService.save(ipInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @Login
    @RequestMapping("/update")
    public R update(@RequestBody IpInfoEntity ipInfo){
        ipInfoService.updateById(ipInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @Login
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        ipInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
